/**
 * @Title: SettingActivity.java
 * @Package: com.jason.fingerprint.ui
 * @Descripton: TODO
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年11月1日 下午4:28:37
 * @Version: V1.0
 */
package com.jason.fingerprint.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jason.fingerprint.AppContext;
import com.jason.fingerprint.R;
import com.jason.fingerprint.beans.VersionBean;
import com.jason.fingerprint.common.DialogHelper;
import com.jason.fingerprint.common.UIHelper;
import com.jason.fingerprint.logic.DownloadSyncLogic;
import com.jason.fingerprint.logic.GetVersionSyncLogic;
import com.jason.fingerprint.widget.LoadingDialog;

import org.kymjs.aframe.ui.BindView;
import org.kymjs.aframe.ui.KJActivityManager;
import org.kymjs.aframe.ui.activity.BaseActivity;

import java.io.File;

/**
 * @ClassName: SettingActivity
 * @Description: 系统设置
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年11月1日 下午4:28:37
 */
public class SettingActivity extends BaseActivity {

    @BindView(id = R.id.header_back, click = true)
    private Button mBackButton;
    @BindView(id = R.id.header_title)
    private TextView mTitleTextView;

    @BindView(id = R.id.setting_layout_help, click = true)
    private RelativeLayout mHelpRelativeLayout;

    @BindView(id = R.id.setting_layout_feedback, click = true)
    private RelativeLayout mFeedbackRelativeLayout;

    @BindView(id = R.id.setting_layout_comment, click = true)
    private RelativeLayout mCommentRelativeLayout;

    @BindView(id = R.id.setting_layout_update, click = true)
    private RelativeLayout mUpdateRelativeLayout;

    @BindView(id = R.id.setting_layout_wifi, click = true)
    private RelativeLayout mWifiRelativeLayout;

    private LoadingDialog mProgressDialog;

    @Override
    public void setRootView() {
        setContentView(R.layout.activity_setting);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mTitleTextView.setVisibility(View.VISIBLE);
        mTitleTextView.setText("系统配置");
        mProgressDialog = new LoadingDialog(this);
        mHelpRelativeLayout.setVisibility(View.GONE);
        mFeedbackRelativeLayout.setVisibility(View.GONE);
        mCommentRelativeLayout.setVisibility(View.GONE);
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()) {
            case R.id.header_back:
                KJActivityManager.create().finishActivity(SettingActivity.class);
                break;
            case R.id.setting_layout_help:
                break;
            case R.id.setting_layout_feedback:
                break;
            case R.id.setting_layout_comment:
                break;
            case R.id.setting_layout_update:
                checkVersion();
                break;
            case R.id.setting_layout_wifi:
                DialogHelper.openNetSettingsDialog(SettingActivity.this);
                break;

            default:
                break;
        }
    }

    private void checkVersion() {
        mProgressDialog.setIsOut(false);
        mProgressDialog.setMessage("加载中...");
        mProgressDialog.show();
        GetVersionSyncLogic logic = new GetVersionSyncLogic(AppContext.getInstance(), mCheckVersionHandler, true);
        logic.execute();
    }


    private Handler mCheckVersionHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (mProgressDialog != null){
                mProgressDialog.dismiss();
            }
            switch (msg.what) {
                case UIHelper.VERSION_SUCCESS:
                    mProgressDialog.setIsOut(false);
                    mProgressDialog.setMessage("请稍等，正在下载中...");
                    mProgressDialog.show();
                    VersionBean bean = (VersionBean) msg.obj;
                    DownloadSyncLogic logic = new DownloadSyncLogic(bean,mDownloadHandler);
                    logic.start();
                    break;
                case UIHelper.VERSION_NO_DATA:
                    Toast.makeText(getApplicationContext(), "当前已经是最新版本", Toast.LENGTH_SHORT).show();
                    break;
                case UIHelper.VERSION_FAIL:
                    Toast.makeText(getApplicationContext(), "更新失败", Toast.LENGTH_SHORT).show();
                    break;
                case UIHelper.HTTP_CODE_TIMEOUT:
                    Toast.makeText(getApplicationContext(), "网络连接超时", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    };

    private Handler mDownloadHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (mProgressDialog != null){
                mProgressDialog.dismiss();
            }
            if (msg.what == 0){
                downloadFinish((String)msg.obj);
            }
        }
    };

    private void downloadFinish(String saveFileName){
        File apkfile = new File(saveFileName);
        if (!apkfile.exists()) {
            return;
        }
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
        startActivity(i);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mProgressDialog != null){
            mProgressDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }
}
